***********************************************
* Title: rwanda_teacherobs_endline_jde.do
* Author: Todd Pugatch
*
* Last update: June 10 2024
* Description: data cleaning for Blimpo and Pugatch, "Entrepreneurship Education
*	and Teacher Training in Rwanda," Stage 2 Registered report, Journal of 
*	Development Economics
*
* Inputs: teacher_monitoring_endline_clean_final_NOPIIs.dta
* Outputs: teacherobs_endline_clean_jde.dta
* Notes: cleans teacher observation endline survey
************************************************

* Set environment
local start=`"$S_TIME"'
clear
clear matrix
clear mata
graph drop _all
program drop _all
cap log close
set more off

* Set directories 
*global main "[SET MAIN DIRECTORY HERE]"
	global rawdata "$main/01_data/01_raw"
	global cleandata "$main/01_data/02_clean"
	global dofiles "$main/02_dofiles"
	global results "$main/03_results"
	global temp "$main/04_output"

* define exchange rate to convert all variables in FRW into USD
local xrate=868.0579 /*exchange rate on 1 July 2018: http://www.exchangerates.org.uk/USD-RWF-exchange-rate-history-full.html*/

/*********************************************************
					LOAD AND PREPARE DATA
variable naming and label conventions: 
	--names:	use [bl/el] suffix for baseline/endline
				omit if variable is an identifier that could be used to match
					across datasets, like treatment status or school code
	--labels: 	use B/E for baseline/endline 
				use [H/T/S][Q/O] for head teacher/teacher/student questionnaire/observation
	--example: enrollment, as reported by head teacher at baseline in item #123
		name: enroll_bl
		label: "BHQ123: enrollment"
************************************************************/ 

qui use "$rawdata/teacher/teacher_monitoring_endline_clean_final_NOPIIs.dta", clear

*************************************************
* 		SECTION 1: identifying information		*
*************************************************
/*survey identifying information: need to mark as endline*/
local X "uniqueid enumerator enum_code consent visitstarth visitstartm reenter_teacherid reenter_schoolid schoolid_bas formdef_version key submissiondate starttime endtime visit_date_112 piiid"
foreach x in `X' {
	qui gen `x'_el=`x'
	qui lab var `x'_el "`x', endline"
}
qui gen long teacherid=reenter_teacherid /*can be used to match with endline teacher survey*/

* school id
qui gen long school_code=. 
qui replace school_code=reenter_schoolid

qui destring schoolid_bas, gen(schoolid_basx)
list school* if school_code!=schoolid_basx
drop schoolid_basx

/*correct school label errors, according to "Manual Checking Report.docx" */
qui gen schoolname_el=school_name_106
lab var schoolname_el "school name, as reported in endline (school_name_106)"
lab val schoolname_el schoollab

/*teacher ID quality check: */
list teacherid reenter_teacherid school_code school_name_106 if teacherid!=reenter_teacherid

qui gen x=substr(uniqueid,1,6) if school_code!=3503001|school_code!=5030403|school_code!=40519134 /*exclude schools with IDs>6 digits*/
qui replace x=substr(uniqueid,1,7) if school_code==3503001|school_code==5030403
qui replace x=substr(uniqueid,1,8) if school_code==40519134
qui destring x, replace
count if school_code!=x & school_code!=.
sort school_code teacherid
list school_code school_name_106 schoolid_bas x teacherid reenter_teacherid if school_code!=x & school_code!=. 
drop x

/*observations for which no matches found in teacher endline survey upon initial merge, but which are likely to have
	miscoded teacherid*/
/*update discrepancies between treatment status between sampling spreadsheet and dataset*/
qui replace schoolgroup_107=2 if school_code==301112 
qui replace schoolgroup_107=1 if school_code==403110 
qui replace schoolgroup_107=1 if school_code==403037 
qui replace schoolgroup_107=1 if school_code==403050 
qui replace schoolgroup_107=1 if school_code==305012	
qui replace schoolgroup_107=1 if school_code==504114	

/*update issues with treatment status that appear later, in merge w/ endline teacher survey*/
qui replace schoolgroup_107=0 if school_code==405124
qui replace schoolgroup_107=0 if school_code==503118 

/*create district & province IDs to match Census/map codes*/
qui gen district_id=.
qui replace district_id=57 if district_101==1 
qui replace district_id=53 if district_101==2 
qui replace district_id=54 if district_101==3 
qui replace district_id=56 if district_101==4 
qui replace district_id=42 if district_101==5 
qui replace district_id=45 if district_101==6 
qui replace district_id=43 if district_101==7 
qui replace district_id=41 if district_101==8 
qui replace district_id=31 if district_101==9 
qui replace district_id=35 if district_101==10 
qui replace district_id=34 if district_101==11 
qui lab var district_id "District ID, Census code"

qui gen province_id=.
qui replace province_id=5 if province_100==1 /*Eastern*/
qui replace province_id=4 if province_100==2 /*Northern*/
qui replace province_id=3 if province_100==3 /*Western*/
qui lab var province_id "Province ID, Census code"

/*school type*/
/*note that public/private status doesn't appear in questionnaire, but copied over from other data*/
qui gen public=(publ_priv_108==1)
qui gen private=(publ_priv_108==2)
qui gen private_pubaided=(publ_priv_108==3)
qui gen nonpublic=(publ_priv_108==2|publ_priv_108==3)
qui gen treatment=(schoolgroup_107==1)
qui replace treatment=. if schoolgroup_107==.
sort district_101 public
qui egen strata=group(district_101 public)
lab def treatment 0 "control" 1 "treatment"
lab val treatment treatment 
qui lab var public "BTQ108: public school"
qui lab var private "BTQ108: private school"
qui lab var private_pubaided "BTQ108: part public, part private school"
qui lab var nonpublic "BTQ108: non-public school"
qui lab var strata "stratification ID (district and public/non-public)"

*************************************************
* 		SECTION 2: immediately upon arrival     *
*************************************************
/*teacher activity*/
qui gen arrival_teacher_teach_el=(teacherdoing_200==1)
qui gen arrival_teacher_organize_el=(teacherdoing_200==2)
qui gen arrival_teacher_discipline_el=(teacherdoing_200==3)
qui gen arrival_teacher_paper_el=(teacherdoing_200==4)
qui gen arrival_teacher_mark_el=(teacherdoing_200==5)
qui gen arrival_teacher_idle_el=(teacherdoing_200==6)
qui gen arrival_teacher_break_el=(teacherdoing_200==7)
qui gen arrival_teacher_out_el=(teacherdoing_200==8)
qui gen arrival_teacher_teachother_el=(teacherdoing_200==9)
qui gen arrival_teacher_absent_el=(teacherdoing_200==10)
qui lab var arrival_teacher_teach_el "ETO200: teaching class upon arrival"
qui lab var arrival_teacher_organize_el "ETO200: organizing class upon arrival"
qui lab var arrival_teacher_discipline_el "ETO200: disciplining class upon arrival"
qui lab var arrival_teacher_paper_el "ETO200: doing paperwork class upon arrival"
qui lab var arrival_teacher_mark_el "ETO200: marking exams/papers upon arrival"
qui lab var arrival_teacher_idle_el "ETO200: idle in classroom upon arrival"
qui lab var arrival_teacher_break_el "ETO200: on break upon arrival"
qui lab var arrival_teacher_out_el "ETO200: out of classroom upon arrival"
qui lab var arrival_teacher_teachother_el "ETO200: teaching another class upon arrival"
qui lab var arrival_teacher_absent_el "ETO200: absent upon arrival"
qui gen arrival_teacher_inclassroom_el=(teacherdoing_200==1|teacherdoing_200==2|teacherdoing_200==3|teacherdoing_200==4|teacherdoing_200==6)
qui gen arrival_teacher_interact_el=(teacherdoing_200==1|teacherdoing_200==2|teacherdoing_200==3)
qui lab var arrival_teacher_inclassroom_el "ETO200: in classroom upon arrival"
qui lab var arrival_teacher_interact_el "ETO200: interacting with students upon arrival"

/*number of students, see also numgirls_201, numboys_202*/
qui gen numstudents_el=numgirls_201+numboys_202
qui lab var numstudents_el "ETO201-202: number of students in classroom"


********************************************************
* 		SECTION 3: Stallings Observation			   *
********************************************************
/*Skills Lab?*/
qui gen skillslab_sched_el=(skills_lab_call==1)
qui gen skillslab_class_el=(skills_lab_class==1)
qui lab var skillslab_sched_el "ETO: observed class scheduled to be Skills Lab"
qui lab var skillslab_class_el "ETO: observed class intended to be Skills Lab"

/* --------- Stallings classroom observations ------*
* code follows rwanda_teacher_monitoring2016T3_modify2.do & rwanda_teacher_monitoring2017T2_modify1.do
* note that these variables don't follow "_el" suffix naming convention for endline, because variable names
	would be too long
* instead, use variable labels starting with "ETO:" to signal that data are from endline
* first, recode "start" observations for use in loops
/*also note that at start, several activity labels differ from what is used in subsequent snapshot, though
	they mean the same thing:
	"demo" labeled as "teach_present" 
	"qanda" labeled as "discus_ques"
	"present" labeled as "stud_present"
	but these are same thing*/
	
*****************************************************/

foreach x in teacher student { /*fix for loops*/
	qui gen start_demo_`x'=start_teach_present_`x'
	qui gen start_qanda_`x'=start_discus_ques_`x'
	qui gen start_present_`x'=start_stud_present_`x'
}

/*Stallings: TEACHER BEHAVIOR*/
/*Note that teacher behavior was coded incorrectly. Teachers are supposed to be involved in only one activity at a 
	snapshot, but many teachers recorded as being involved in multiple activities. Use definition of "main activity"
	from Monitoring Round 1, Term 3 2016. */
* fix variable names for loops	
local action "demo qanda present pradrill pwork silentwork copying assign uninv classmgt outroom"
foreach a in `action' {
	qui gen o0min_`a'_teacher=start_`a'_teacher
}




forval m=0(4)48 {
	/*create dummy for whether teacher engaged in an action or not*/
	foreach a in `action' {
		qui gen teacher_`a'_`m'min=(o`m'min_`a'_teacher!=5)
		qui replace teacher_`a'_`m'min=. if o`m'min_`a'_teacher==.
	}

	/*Teacher should only be doing one activity at a time. 
		Check if teacher coded as involved in more than one activity*/
	qui egen numact_teacher_`m'min=rowtotal(teacher_*_`m'min)
	di "distribution of number of teacher activities at minute `m'"
	tab numact_teacher_`m'min, mi
	
	/*Find the teacher activity(ies) with maximum number of students participating*/
	foreach a in `action' {
		qui gen o`m'min_`a'_teacher_recode=o`m'min_`a'_teacher if o`m'min_`a'_teacher!=5 /*disregard "not happening" code*/
	}	
	qui egen x=rowmax(o`m'min_*_teacher_recode) if numact_teacher_`m'min>1

	/*set activity with most student involvement as main activity (multiple activities possible)*/
	foreach a in `action' {
		qui gen mainact_teacher_`a'_`m'min=0 if teacher_`a'_`m'min!=.
		/*teachers with just one activity coded (as instructed)*/
		qui replace mainact_teacher_`a'_`m'min=teacher_`a'_`m'min if numact_teacher_`m'min==1	
		/*teachers with multiple activities coded*/
		qui replace mainact_teacher_`a'_`m'min=teacher_`a'_`m'min if numact_teacher_`m'min>1 & o`m'min_`a'_teacher_recode==x
		/*teachers with no activities at all*/
		qui replace mainact_teacher_`a'_`m'min=. if numact_teacher_`m'min==0
	}

	qui egen nummainact_teacher_`m'min=rowtotal(mainact_teacher_*_`m'min) /*number of main activities*/
	qui gen multmainact_teacher_`m'min=(nummainact_teacher_`m'min>1 & nummainact_teacher_`m'min!=.) /*indicator for multiple main activities*/
	drop x *recode
	
	/*classify activities into groups (classifications based on E! Monitoring_Training Manual_Sept 2016.pdf, p. 19-21)
		and JDE Stage 1 Registered Report:
		active instruction: discussion/questions and answers, student presentation, project/interactive activity
		passive instruction: demonstration/lecture, practice/drill, silent seat work, copying
		classroom management: instructions for assignments, class management
		off task: uninvolved, out of room*/
	qui egen teacher_active_`m'min=anymatch(teacher_qanda_`m'min teacher_present_`m'min teacher_pwork_`m'min), v(1)
	qui egen teacher_passive_`m'min=anymatch(teacher_demo_`m'min teacher_pradrill_`m'min teacher_silentwork_`m'min teacher_copying_`m'min), v(1)
	qui egen teacher_mgmt_`m'min=anymatch(teacher_assign_`m'min teacher_classmgt_`m'min), v(1)
	qui egen teacher_offtask_`m'min=anymatch(teacher_uninv_`m'min teacher_outroom_`m'min), v(1)
	qui egen teacher_instruct_`m'min=anymatch(teacher_active_`m'min teacher_passive_`m'min), v(1)
	qui egen mainact_teacher_active_`m'min=anymatch(mainact_teacher_qanda_`m'min mainact_teacher_present_`m'min mainact_teacher_pwork_`m'min), v(1)
	qui egen mainact_teacher_passive_`m'min=anymatch(mainact_teacher_demo_`m'min mainact_teacher_pradrill_`m'min mainact_teacher_silentwork_`m'min mainact_teacher_copying_`m'min), v(1)
	qui egen mainact_teacher_mgmt_`m'min=anymatch(mainact_teacher_assign_`m'min mainact_teacher_classmgt_`m'min), v(1)
	qui egen mainact_teacher_offtask_`m'min=anymatch(mainact_teacher_uninv_`m'min mainact_teacher_outroom_`m'min), v(1)
	qui egen mainact_teacher_instruct_`m'min=anymatch(mainact_teacher_active_`m'min mainact_teacher_passive_`m'min), v(1)
	
	/*set activity to missing if all activities missing*/
	qui egen x=rownonmiss(mainact_teacher_qanda_`m'min mainact_teacher_present_`m'min 					///
	mainact_teacher_pwork_`m'min mainact_teacher_demo_`m'min mainact_teacher_pradrill_`m'min 			///
	mainact_teacher_silentwork_`m'min mainact_teacher_copying_`m'min mainact_teacher_assign_`m'min 		///
	mainact_teacher_classmgt_`m'min mainact_teacher_uninv_`m'min mainact_teacher_outroom_`m'min)		///
	
	qui egen y=rownonmiss(teacher_qanda_`m'min teacher_present_`m'min teacher_pwork_`m'min teacher_demo_`m'min ///
	teacher_pradrill_`m'min teacher_silentwork_`m'min teacher_copying_`m'min teacher_assign_`m'min 			   ///
	teacher_classmgt_`m'min teacher_uninv_`m'min teacher_outroom_`m'min)
	
	qui egen y2=rowtotal(teacher_qanda_`m'min teacher_present_`m'min teacher_pwork_`m'min teacher_demo_`m'min  ///
	teacher_pradrill_`m'min teacher_silentwork_`m'min teacher_copying_`m'min teacher_assign_`m'min 			   ///
	teacher_classmgt_`m'min teacher_uninv_`m'min teacher_outroom_`m'min) /*instead of rescaling*/
	
	foreach x in active passive mgmt offtask instruct {
		qui replace mainact_teacher_`x'_`m'min=. if x==0
		qui replace teacher_`x'_`m'min=. if y==0|y2==0
	}
	drop x y y2
	
	/*label variables*/
	qui lab var teacher_demo_`m'min "ETO: teacher demonstration/lecture at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_qanda_`m'min "ETO: teacher Q&A/discussion at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_present_`m'min "ETO: teacher has students present to class at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_pradrill_`m'min "ETO: teacher practice/drill at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_pwork_`m'min "ETO: teacher project/interactive activity at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_silentwork_`m'min "ETO: teacher silent seat work at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_copying_`m'min "ETO: teacher copying at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_assign_`m'min "ETO: teacher instructions for assignments at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_uninv_`m'min "ETO: teacher uninvolved at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_classmgt_`m'min "ETO: teacher classroom management at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_outroom_`m'min "ETO: teacher out of room at `m' min. elapsed (allows multiple activities)"
	qui lab var mainact_teacher_demo_`m'min "ETO: teacher main activity: demonstration/lecture at `m' min. elapsed"
	qui lab var mainact_teacher_qanda_`m'min "ETO: teacher main activity: Q&A/discussion at `m' min. elapsed"
	qui lab var mainact_teacher_present_`m'min "ETO: teacher main activity: has students present to class at `m' min. elapsed"
	qui lab var mainact_teacher_pradrill_`m'min "ETO: teacher main activity: practice/drill at `m' min. elapsed"
	qui lab var mainact_teacher_pwork_`m'min "ETO: teacher main activity: project/interactive activity at `m' min. elapsed"
	qui lab var mainact_teacher_silentwork_`m'min "ETO: teacher main activity: silent seat work at `m' min. elapsed"
	qui lab var mainact_teacher_copying_`m'min "ETO: teacher main activity: copying at `m' min. elapsed"
	qui lab var mainact_teacher_assign_`m'min "ETO: teacher main activity: instructions for assignments at `m' min. elapsed"
	qui lab var mainact_teacher_uninv_`m'min "ETO: teacher main activity: uninvolved at `m' min. elapsed"
	qui lab var mainact_teacher_classmgt_`m'min "ETO: teacher main activity: classroom management at `m' min. elapsed"
	qui lab var mainact_teacher_outroom_`m'min "ETO: teacher main activity: out of room at `m' min. elapsed"
	qui lab var numact_teacher_`m'min "ETO: number of teacher activities at `m' min. elapsed"
	qui lab var nummainact_teacher_`m'min "ETO: number of teacher main activities (based on student involvement) at `m' min. elapsed"
	qui lab var multmainact_teacher_`m'min "ETO: multiple teacher main activities (based on student involvement) at `m' min. elapsed"
	qui lab var teacher_active_`m'min "ETO: teacher active instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_passive_`m'min "ETO: teacher passive instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_mgmt_`m'min "ETO: teacher clasroom management at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_offtask_`m'min "ETO: teacher off task at `m' min. elapsed (allows multiple activities)"
	qui lab var teacher_instruct_`m'min "ETO: teacher instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var mainact_teacher_active_`m'min "ETO: teacher main activity: active instruction at `m' min. elapsed"
	qui lab var mainact_teacher_passive_`m'min "ETO: teacher main activity: passive instruction at `m' min. elapsed"
	qui lab var mainact_teacher_mgmt_`m'min "ETO: teacher main activity: clasroom management at `m' min. elapsed"
	qui lab var mainact_teacher_offtask_`m'min "ETO: teacher main activity: off task at `m' min. elapsed"
	qui lab var mainact_teacher_instruct_`m'min "ETO: teacher main activity: instruction at `m' min. elapsed"
}

/*now find proportion of time spent in each activity*/
/*calculate proportions at different intervals: 
	overall
	0-27 minutes, 28-52 minutes (1st/2nd half of class observation)
	0-11 minutes, 12-27 minutes, 28-39 minutes, 40-52 minutes (roughly quarters of class observation)*/
/*rescale main activities so that proportions sum to 1*/
/*rescale "any activity" so that proportions sum to at least 1*/
local category "active passive mgmt offtask"
foreach a in `action' `category' {
	order teacher_`a'_*min mainact_teacher_`a'_*min
	qui egen teacher_`a'_pct=rowmean(teacher_`a'_0min-teacher_`a'_48min)
	qui egen teacher_`a'_pct_m0_27=rowmean(teacher_`a'_0min-teacher_`a'_24min)
	qui egen teacher_`a'_pct_m28_52=rowmean(teacher_`a'_28min-teacher_`a'_48min)
	qui egen teacher_`a'_pct_m0_11=rowmean(teacher_`a'_0min-teacher_`a'_8min)
	qui egen teacher_`a'_pct_m12_27=rowmean(teacher_`a'_12min-teacher_`a'_24min)
	qui egen teacher_`a'_pct_m28_39=rowmean(teacher_`a'_28min-teacher_`a'_36min)
	qui egen teacher_`a'_pct_m40_52=rowmean(teacher_`a'_40min-teacher_`a'_48min)
	/*placeholders for later rescaling*/
	qui egen mainact_`a'_pctx=rowmean(mainact_teacher_`a'_0min-mainact_teacher_`a'_48min)
	qui egen mainact_`a'_0_27x=rowmean(mainact_teacher_`a'_0min-mainact_teacher_`a'_24min)
	qui egen mainact_`a'_28_52x=rowmean(mainact_teacher_`a'_28min-mainact_teacher_`a'_48min)
	qui egen mainact_`a'_0_11x=rowmean(mainact_teacher_`a'_0min-mainact_teacher_`a'_8min)
	qui egen mainact_`a'_12_27x=rowmean(mainact_teacher_`a'_12min-mainact_teacher_`a'_24min)
	qui egen mainact_`a'_28_39x=rowmean(mainact_teacher_`a'_28min-mainact_teacher_`a'_36min)
	qui egen mainact_`a'_40_52x=rowmean(mainact_teacher_`a'_40min-mainact_teacher_`a'_48min)
}

/*rescale main activity*/
foreach x in pct 0_27 28_52 0_11 12_27 28_39 40_52 {
	/*individual activities*/
	local ind "mainact_demo_`x'x mainact_qanda_`x'x mainact_present_`x'x mainact_pradrill_`x'x mainact_pwork_`x'x mainact_silentwork_`x'x mainact_copying_`x'x mainact_assign_`x'x mainact_uninv_`x'x mainact_classmgt_`x'x mainact_outroom_`x'x"
	/*aggregate activities*/
	local agg "mainact_mgmt_`x'x mainact_offtask_`x'x mainact_active_`x'x mainact_passive_`x'x"
	/*total proportion (can sum to more than 1)*/
	qui egen mainact_ind_`x'tot=rowtotal(`ind')
	qui egen mainact_agg_`x'tot=rowtotal(`agg')
	/*rescaling so that proportions sum to 1*/
	foreach a in `action' {
		qui gen mainact_teacher_`a'_`x'=mainact_`a'_`x'x/mainact_ind_`x'tot
		qui replace mainact_teacher_`a'_`x'=. if mainact_ind_`x'tot==0
	}
	foreach a in `category' {
		qui gen mainact_teacher_`a'_`x'=mainact_`a'_`x'x/mainact_agg_`x'tot
		qui replace mainact_teacher_`a'_`x'=. if mainact_agg_`x'tot==0
	}
	qui gen mainact_teacher_instruct_`x'=mainact_teacher_active_`x' + mainact_teacher_passive_`x'
	/*check that rescaling worked*/
	local ind "mainact_teacher_demo_`x' mainact_teacher_qanda_`x' mainact_teacher_present_`x' mainact_teacher_pradrill_`x' mainact_teacher_pwork_`x' mainact_teacher_silentwork_`x' mainact_teacher_copying_`x' mainact_teacher_assign_`x' mainact_teacher_uninv_`x' mainact_teacher_classmgt_`x' mainact_teacher_outroom_`x'"
	local agg "mainact_teacher_mgmt_`x' mainact_teacher_offtask_`x' mainact_teacher_instruct_`x'"
	qui egen ind_`x'tot=rowtotal(`ind'), missing
	qui egen agg_`x'tot=rowtotal(`agg'), missing
	su ind_`x'tot agg_`x'tot
	drop *`x'x *`x'tot
}

/*total instructional time (any activity)*/
order teacher_instruct_*min mainact_teacher_instruct_*min
qui egen teacher_instruct_pct=rowmean(teacher_instruct_0min-teacher_instruct_48min)
qui egen teacher_instruct_pct_m0_27=rowmean(teacher_instruct_0min-teacher_instruct_24min)
qui egen teacher_instruct_pct_m28_52=rowmean(teacher_instruct_28min-teacher_instruct_48min)
qui egen teacher_instruct_pct_m0_11=rowmean(teacher_instruct_0min-teacher_instruct_8min)
qui egen teacher_instruct_pct_m12_27=rowmean(teacher_instruct_12min-teacher_instruct_24min)
qui egen teacher_instruct_pct_m28_39=rowmean(teacher_instruct_28min-teacher_instruct_36min)
qui egen teacher_instruct_pct_m40_52=rowmean(teacher_instruct_40min-teacher_instruct_48min)
foreach x in pct pct_m0_27 pct_m28_52 pct_m0_11 pct_m12_27 pct_m28_39 pct_m40_52 {
	qui replace teacher_instruct_`x'=. if teacher_active_`x'==. & teacher_passive_`x'==.
}

/*DON'T RESCALE "ANY ACTIVITY," BECAUSE LOGICALLY INCONSISTENT. Percentages can sum to >100%.*/
order nummainact_teacher_*min multmainact_teacher_*min
qui egen nummainact_teacher_avg=rowmean(nummainact_teacher_0min-nummainact_teacher_36min)
qui egen multmainact_teacher_pct=rowmean(multmainact_teacher_0min-multmainact_teacher_36min)
qui lab var teacher_demo_pct "ETO: teacher proportion of time in demonstration/lecture (can sum to >100%)"
qui lab var teacher_qanda_pct "ETO: teacher proportion of time in Q&A/discussion (can sum to >100%)"
qui lab var teacher_present_pct "ETO: teacher proportion of time in student presentation (can sum to >100%)"
qui lab var teacher_pradrill_pct "ETO: teacher proportion of time in practice/drill (can sum to >100%)"
qui lab var teacher_pwork_pct "ETO: teacher proportion of time in project/interactive activity (can sum to >100%)"
qui lab var teacher_silentwork_pct "ETO: teacher proportion of time in silent seat work (can sum to >100%)"
qui lab var teacher_copying_pct "ETO: teacher proportion of time in copying (can sum to >100%)"
qui lab var teacher_assign_pct "ETO: teacher proportion of time in instructions for assignments (can sum to >100%)"
qui lab var teacher_uninv_pct "ETO: teacher proportion of time uninvolved (can sum to >100%)"
qui lab var teacher_classmgt_pct "ETO: teacher proportion of time in classroom management (can sum to >100%)"
qui lab var teacher_outroom_pct "ETO: teacher proportion of time out of room (can sum to >100%)"
qui lab var teacher_active_pct "ETO: teacher proportion of time in active instruction (can sum to >100%)"
qui lab var teacher_passive_pct "ETO: teacher proportion of time in passive instruction (can sum to >100%)"
qui lab var teacher_mgmt_pct "ETO: teacher proportion of time in classroom management (major category, can sum to >100%)"
qui lab var teacher_offtask_pct "ETO: teacher proportion of time off task (can sum to >100%)"
qui lab var teacher_instruct_pct "ETO: teacher proportion of time in instruction (can sum to >100%)"
qui lab var nummainact_teacher_avg "ETO: average number of teacher main activities (based on student involvement)"
qui lab var multmainact_teacher_pct "ETO: % of snapshots w/ multiple teacher main activities (based on student involvement)"	
	
foreach x in 0_27 28_52 0_11 12_27 28_39 40_52 {
	qui lab var teacher_demo_pct_m`x' "ETO: teacher proportion of time in demonstration/lecture, mins `x' (can sum to >100%)"
	qui lab var teacher_qanda_pct_m`x' "ETO: teacher proportion of time in Q&A/discussion, mins `x' (can sum to >100%)"
	qui lab var teacher_present_pct_m`x' "ETO: teacher proportion of time in student presentation, mins `x' (can sum to >100%)"
	qui lab var teacher_pradrill_pct_m`x' "ETO: teacher proportion of time in practice/drill, mins `x' (can sum to >100%)"
	qui lab var teacher_pwork_pct_m`x' "ETO: teacher proportion of time in project/interactive activity, mins `x' (can sum to >100%)"
	qui lab var teacher_silentwork_pct_m`x' "ETO: teacher proportion of time in silent seat work, mins `x' (can sum to >100%)"
	qui lab var teacher_copying_pct_m`x' "ETO: teacher proportion of time in copying, mins `x' (can sum to >100%)"
	qui lab var teacher_assign_pct_m`x' "ETO: teacher proportion of time in instructions for assignments, mins `x' (can sum to >100%)"
	qui lab var teacher_uninv_pct_m`x' "ETO: teacher proportion of time uninvolved, mins `x' (can sum to >100%)"
	qui lab var teacher_classmgt_pct_m`x' "ETO: teacher proportion of time in classroom management, mins `x' (can sum to >100%)"
	qui lab var teacher_outroom_pct_m`x' "ETO: teacher proportion of time out of room, mins `x' (can sum to >100%)"
	qui lab var teacher_active_pct_m`x' "ETO: teacher proportion of time in active instruction, mins `x' (can sum to >100%)"
	qui lab var teacher_passive_pct_m`x' "ETO: teacher proportion of time in passive instruction, mins `x' (can sum to >100%)"
	qui lab var teacher_mgmt_pct_m`x' "ETO: teacher proportion of time in classroom management, mins `x' (major category, can sum to >100%)"
	qui lab var teacher_offtask_pct_m`x' "ETO: teacher proportion of time off task, mins `x' (can sum to >100%)"
	qui lab var teacher_instruct_pct_m`x' "ETO: teacher proportion of time in instruction, mins `x' (can sum to >100%)"
}

qui lab var mainact_teacher_demo_pct "ETO: teacher main activity proportion of time in demonstration/lecture (rescaled)"
qui lab var mainact_teacher_qanda_pct "ETO: teacher main activity proportion of time in Q&A/discussion (rescaled)"
qui lab var mainact_teacher_present_pct "ETO: teacher main activity proportion of time in student presentation (rescaled)"
qui lab var mainact_teacher_pradrill_pct "ETO: teacher main activity proportion of time in practice/drill (rescaled)"
qui lab var mainact_teacher_pwork_pct "ETO: teacher main activity proportion of time in project/interactive activity (rescaled)"
qui lab var mainact_teacher_silentwork_pct "ETO: teacher main activity proportion of time in silent seat work (rescaled)"
qui lab var mainact_teacher_copying_pct "ETO: teacher main activity proportion of time in copying (rescaled)"
qui lab var mainact_teacher_assign_pct "ETO: teacher main activity proportion of time in instructions for assignments (rescaled)"
qui lab var mainact_teacher_uninv_pct "ETO: teacher main activity proportion of time uninvolved (rescaled)"
qui lab var mainact_teacher_classmgt_pct "ETO: teacher main activity proportion of time in classroom management (rescaled)"
qui lab var mainact_teacher_outroom_pct "ETO: teacher main activity proportion of time out of room (rescaled)"
qui lab var mainact_teacher_active_pct "ETO: teacher main activity proportion of time in active instruction (rescaled)"
qui lab var mainact_teacher_passive_pct "ETO: teacher main activity proportion of time in passive instruction (rescaled)"
qui lab var mainact_teacher_mgmt_pct "ETO: teacher main activity proportion of time in classroom management (major category, rescaled)"
qui lab var mainact_teacher_offtask_pct "ETO: teacher main activity proportion of time off task (rescaled)"
qui lab var mainact_teacher_instruct_pct "ETO: teacher main activity proportion of time in instruction (rescaled)"

foreach x in 0_27 28_52 0_11 12_27 28_39 40_52 {
	qui lab var mainact_teacher_demo_`x' "ETO: teacher main activity proportion of time in demonstration/lecture, mins `x' (rescaled)"
	qui lab var mainact_teacher_qanda_`x' "ETO: teacher main activity proportion of time in Q&A/discussion, mins `x' (rescaled)"
	qui lab var mainact_teacher_present_`x' "ETO: teacher main activity proportion of time in student presentation, mins `x' (rescaled)"
	qui lab var mainact_teacher_pradrill_`x' "ETO: teacher main activity proportion of time in practice/drill, mins `x' (rescaled)"
	qui lab var mainact_teacher_pwork_`x' "ETO: teacher main activity proportion of time in project/interactive activity, mins `x' (rescaled)"
	qui lab var mainact_teacher_silentwork_`x' "ETO: teacher main activity proportion of time in silent seat work, mins `x' (rescaled)"
	qui lab var mainact_teacher_copying_`x' "ETO: teacher main activity proportion of time in copying, mins `x' (rescaled)"
	qui lab var mainact_teacher_assign_`x' "ETO: teacher main activity proportion of time in instructions for assignments, mins `x' (rescaled)"
	qui lab var mainact_teacher_uninv_`x' "ETO: teacher main activity proportion of time uninvolved, mins `x' (rescaled)"
	qui lab var mainact_teacher_classmgt_`x' "ETO: teacher main activity proportion of time in classroom management, mins `x' (rescaled)"
	qui lab var mainact_teacher_outroom_`x' "ETO: teacher main activity proportion of time out of room, mins `x' (rescaled)"
	qui lab var mainact_teacher_active_`x' "ETO: teacher main activity proportion of time in active instruction, mins `x' (rescaled)"
	qui lab var mainact_teacher_passive_`x' "ETO: teacher main activity proportion of time in passive instruction, mins `x' (rescaled)"
	qui lab var mainact_teacher_mgmt_`x' "ETO: teacher main activity proportion of time in classroom management (major category), mins `x' (rescaled)"
	qui lab var mainact_teacher_offtask_`x' "ETO: teacher main activity proportion of time off task, mins `x' (rescaled)"
	qui lab var mainact_teacher_instruct_`x' "ETO: teacher main activity proportion of time in instruction, mins `x' (rescaled)"
}

/*Stallings: STUDENT BEHAVIOR*/
* Student behavior
/*NOTE THAT STUDENT OBSERVATION CODING DIFFERENT FROM TEACHER OBSERVATION CODING!
		1. 1 student
		2. Small group of students
		3. Large group of students
		4. Not happened
		5. Everyone/Not Applicable (b/c E for teacher)
		--"5" means that teacher was engaging in the activity w/ all students, hence students engaged in activitiy too*/
/*Code in same way as teacher behavior, with minor differences:
	1) For students, different groups could be participating in multiple activities simultaneously, so this is not
		a logical inconsistency as with the teacher data. Still, assign "main activity" to students based on maximal
		number of students reported in an activity, then rescale main activities so that proportions sum to 1.
	2) "Out of room" was not an option for student behavior.*/
local action "demo qanda present pradrill pwork silentwork copying assign uninv classmgt"
foreach a in `action' { /*for loop below*/
	qui gen o0min_`a'_student=start_`a'_student
}

forval m=0(4)48 {
	/*create dummy for whether students engaged in an action or not*/
	foreach a in `action' {
		qui gen student_`a'_`m'min=(o`m'min_`a'_student!=4)
		qui replace student_`a'_`m'min=. if o`m'min_`a'_student==.
	}

	/*Check if students	coded as involved in more than one activity*/
	qui egen numact_student_`m'min=rowtotal(student_*_`m'min)

	/*Find the student activity(ies) with maximum number of students participating*/
	foreach a in `action' {
		qui gen o`m'min_`a'_student_recode=o`m'min_`a'_student if o`m'min_`a'_student!=4 /*disregard "not happening" code*/
	}	
	qui egen x=rowmax(o`m'min_*_student_recode) if numact_student_`m'min>1

	/*set activity with most student involvement as main activity (multiple activities possible)*/
	/*possibility of multiple main activities a distinction from rwanda_teacher_monitoring2016T3_modify1.do*/
	foreach a in `action' {
		qui gen mainact_student_`a'_`m'min=0 if student_`a'_`m'min!=.
		
		/*just one sstudent activity coded*/
		qui replace mainact_student_`a'_`m'min=student_`a'_`m'min if numact_student_`m'min==1
		
		/*multiple student activities coded*/
		qui replace mainact_student_`a'_`m'min=student_`a'_`m'min if numact_student_`m'min>1 & o`m'min_`a'_student_recode==x
		
		/*no student activities at all*/
		qui replace mainact_student_`a'_`m'min=. if numact_student_`m'min==0
	}

	qui egen nummainact_student_`m'min=rowtotal(mainact_*_`m'min) /*number of main activities*/
	qui gen multmainact_student_`m'min=(nummainact_student_`m'min>1 & nummainact_student_`m'min!=.) /*indicator for multiple main activities*/
	drop x *recode

	/*classify activities into groups (classifications based on E! Monitoring_Training Manual_Sept 2016.pdf, p. 19-21)
		and JDE Registered Report:
		active instruction: discussion/questions and answers, student presentation, project/interactive activity
		passive instruction: demonstration/lecture, practice/drill, silent seat work, copying
		classroom management: instructions for assignments, class management
		off task: uninvolved, out of room*/
	qui egen student_active_`m'min=anymatch(student_qanda_`m'min student_present_`m'min student_pwork_`m'min), v(1)
	qui egen student_passive_`m'min=anymatch(student_demo_`m'min student_pradrill_`m'min student_silentwork_`m'min student_copying_`m'min), v(1)
	qui egen student_mgmt_`m'min=anymatch(student_assign_`m'min student_classmgt_`m'min), v(1)
	qui egen student_offtask_`m'min=anymatch(student_uninv_`m'min), v(1)
	qui egen student_instruct_`m'min=anymatch(student_active_`m'min student_passive_`m'min), v(1)
	qui egen mainact_student_active_`m'min=anymatch(mainact_student_qanda_`m'min mainact_student_present_`m'min mainact_student_pwork_`m'min), v(1)
	qui egen mainact_student_passive_`m'min=anymatch(mainact_student_demo_`m'min mainact_student_pradrill_`m'min mainact_student_silentwork_`m'min mainact_student_copying_`m'min), v(1)
	qui egen mainact_student_mgmt_`m'min=anymatch(mainact_student_assign_`m'min mainact_student_classmgt_`m'min), v(1)
	qui egen mainact_student_offtask_`m'min=anymatch(mainact_student_uninv_`m'min), v(1)
	qui egen mainact_student_instruct_`m'min=anymatch(mainact_student_active_`m'min mainact_student_passive_`m'min), v(1)
	/*set activity to missing if all activities missing*/
	qui egen x=rownonmiss(mainact_student_qanda_`m'min mainact_student_present_`m'min 			   ///
	mainact_student_pwork_`m'min mainact_student_demo_`m'min mainact_student_pradrill_`m'min 	   ///
	mainact_student_silentwork_`m'min mainact_student_copying_`m'min mainact_student_assign_`m'min ///
	mainact_student_classmgt_`m'min mainact_student_uninv_`m'min)
	
	qui egen y=rownonmiss(student_qanda_`m'min student_present_`m'min student_pwork_`m'min 		   ///
	student_demo_`m'min student_pradrill_`m'min student_silentwork_`m'min student_copying_`m'min   ///
	student_assign_`m'min student_classmgt_`m'min student_uninv_`m'min)
	
	qui egen y2=rowtotal(student_qanda_`m'min student_present_`m'min student_pwork_`m'min 		   ///
	student_demo_`m'min student_pradrill_`m'min student_silentwork_`m'min student_copying_`m'min   ///
	student_assign_`m'min student_classmgt_`m'min student_uninv_`m'min) /*instead of rescaling*/
	
	foreach x in active passive mgmt offtask instruct {
		qui replace mainact_student_`x'_`m'min=. if x==0
		qui replace student_`x'_`m'min=. if y==0|y2==0
	}
	drop x y y2
	
	/*label variables*/
	qui lab var student_demo_`m'min "ETO: student demonstration/lecture at `m' min. elapsed (allows multiple activities)"
	qui lab var student_qanda_`m'min "ETO: student Q&A/discussion at `m' min. elapsed (allows multiple activities)"
	qui lab var student_present_`m'min "ETO: student has students present to class at `m' min. elapsed (allows multiple activities)"
	qui lab var student_pradrill_`m'min "ETO: student practice/drill at `m' min. elapsed (allows multiple activities)"
	qui lab var student_pwork_`m'min "ETO: student project/interactive activity at `m' min. elapsed (allows multiple activities)"
	qui lab var student_silentwork_`m'min "ETO: student silent seat work at `m' min. elapsed (allows multiple activities)"
	qui lab var student_copying_`m'min "ETO: student copying at `m' min. elapsed (allows multiple activities)"
	qui lab var student_assign_`m'min "ETO: student instructions for assignments at `m' min. elapsed (allows multiple activities)"
	qui lab var student_uninv_`m'min "ETO: student uninvolved at `m' min. elapsed (allows multiple activities)"
	qui lab var student_classmgt_`m'min "ETO: student classroom management at `m' min. elapsed (allows multiple activities)"
	qui lab var mainact_student_demo_`m'min "ETO: student main activity: demonstration/lecture at `m' min. elapsed"
	qui lab var mainact_student_qanda_`m'min "ETO: student main activity: Q&A/discussion at `m' min. elapsed"
	qui lab var mainact_student_present_`m'min "ETO: student main activity: has students present to class at `m' min. elapsed"
	qui lab var mainact_student_pradrill_`m'min "ETO: student main activity: practice/drill at `m' min. elapsed"
	qui lab var mainact_student_pwork_`m'min "ETO: student main activity: project/interactive activity at `m' min. elapsed"
	qui lab var mainact_student_silentwork_`m'min "ETO: student main activity: silent seat work at `m' min. elapsed"
	qui lab var mainact_student_copying_`m'min "ETO: student main activity: copying at `m' min. elapsed"
	qui lab var mainact_student_assign_`m'min "ETO: student main activity: instructions for assignments at `m' min. elapsed"
	qui lab var mainact_student_uninv_`m'min "ETO: student main activity: uninvolved at `m' min. elapsed"
	qui lab var mainact_student_classmgt_`m'min "ETO: student main activity: classroom management at `m' min. elapsed"
	qui lab var numact_student_`m'min "ETO: number of student activities at `m' min. elapsed"
	qui lab var nummainact_student_`m'min "ETO: number of student main activities at `m' min. elapsed"
	qui lab var multmainact_student_`m'min "ETO: multiple student main activities at `m' min. elapsed"
	qui lab var student_active_`m'min "ETO: student active instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var student_passive_`m'min "ETO: student passive instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var student_mgmt_`m'min "ETO: student clasroom management at `m' min. elapsed (allows multiple activities)"
	qui lab var student_offtask_`m'min "ETO: student off task at `m' min. elapsed (allows multiple activities)"
	qui lab var student_instruct_`m'min "ETO: student instruction at `m' min. elapsed (allows multiple activities)"
	qui lab var mainact_student_active_`m'min "ETO: student main activity: active instruction at `m' min. elapsed"
	qui lab var mainact_student_passive_`m'min "ETO: student main activity: passive instruction at `m' min. elapsed"
	qui lab var mainact_student_mgmt_`m'min "ETO: student main activity: clasroom management at `m' min. elapsed"
	qui lab var mainact_student_offtask_`m'min "ETO: student main activity: off task at `m' min. elapsed"
	qui lab var mainact_student_instruct_`m'min "ETO: student main activity: instruction at `m' min. elapsed"
}

/*now find proportion of time spent in each activity*/
/*calculate proportions at different intervals: 
	overall
	0-27 minutes, 28-52 minutes (1st/2nd half of class observation)
	0-11 minutes, 12-27 minutes, 28-39 minutes, 40-52 minutes (roughly quarters of class observation)*/
/*rescale main activity so that totals sum to 1*/
/*rescale "any activity" so that proportions sum to at least 1*/
local category "active passive mgmt offtask"
foreach a in `action' `category' {
	order student_`a'_*min mainact_student_`a'_*min
	qui egen student_`a'_pct=rowmean(student_`a'_0min-student_`a'_48min)
	qui egen student_`a'_pct_m0_27=rowmean(student_`a'_0min-student_`a'_24min)
	qui egen student_`a'_pct_m28_52=rowmean(student_`a'_28min-student_`a'_48min)
	qui egen student_`a'_pct_m0_11=rowmean(student_`a'_0min-student_`a'_8min)
	qui egen student_`a'_pct_m12_27=rowmean(student_`a'_12min-student_`a'_24min)
	qui egen student_`a'_pct_m28_39=rowmean(student_`a'_28min-student_`a'_36min)
	qui egen student_`a'_pct_m40_52=rowmean(student_`a'_40min-student_`a'_48min)
	/*placeholders for later rescaling*/
	qui egen mainact_`a'_pctx=rowmean(mainact_student_`a'_0min-mainact_student_`a'_48min)
	qui egen mainact_`a'_0_27x=rowmean(mainact_student_`a'_0min-mainact_student_`a'_24min)
	qui egen mainact_`a'_28_52x=rowmean(mainact_student_`a'_20min-mainact_student_`a'_48min)
	qui egen mainact_`a'_0_11x=rowmean(mainact_student_`a'_0min-mainact_student_`a'_8min)
	qui egen mainact_`a'_12_27x=rowmean(mainact_student_`a'_12min-mainact_student_`a'_24min)
	qui egen mainact_`a'_28_39x=rowmean(mainact_student_`a'_28min-mainact_student_`a'_36min)
	qui egen mainact_`a'_40_52x=rowmean(mainact_student_`a'_40min-mainact_student_`a'_48min)
}

/*rescale main activity so that totals sum to 1*/
foreach x in pct 0_27 28_52 0_11 12_27 28_39 40_52 {
	/*individual activities*/
	local ind "mainact_demo_`x'x mainact_qanda_`x'x mainact_present_`x'x mainact_pradrill_`x'x mainact_pwork_`x'x mainact_silentwork_`x'x mainact_copying_`x'x mainact_assign_`x'x mainact_uninv_`x'x mainact_classmgt_`x'x"
	/*aggregate activities*/
	local agg "mainact_mgmt_`x'x mainact_offtask_`x'x mainact_active_`x'x mainact_passive_`x'x"
	
	/*total proportion (can sum to more than 1)*/
	qui egen mainact_ind_`x'tot=rowtotal(`ind')
	qui egen mainact_agg_`x'tot=rowtotal(`agg')
	
	/*rescaling so that proportions sum to 1*/
	foreach a in `action' {
		qui gen mainact_student_`a'_`x'=mainact_`a'_`x'x/mainact_ind_`x'tot
		qui replace mainact_student_`a'_`x'=. if mainact_ind_`x'tot==0
		if ("`x'"=="pct") qui replace student_`a'_`x'=. if mainact_ind_`x'tot==0
		else qui replace student_`a'_pct_m`x'=. if mainact_ind_`x'tot==0
	}
	
	foreach a in `category' {
		qui gen mainact_student_`a'_`x'=mainact_`a'_`x'x/mainact_agg_`x'tot
		qui replace mainact_student_`a'_`x'=. if mainact_agg_`x'tot==0
		if ("`x'"=="pct") qui replace student_`a'_`x'=. if mainact_agg_`x'tot==0
		else qui replace student_`a'_pct_m`x'=. if mainact_agg_`x'tot==0
	}
	qui gen mainact_student_instruct_`x'=mainact_student_active_`x' + mainact_student_passive_`x'
	/*check that rescaling worked*/
	local ind "mainact_student_demo_`x' mainact_student_qanda_`x' mainact_student_present_`x' mainact_student_pradrill_`x' mainact_student_pwork_`x' mainact_student_silentwork_`x' mainact_student_copying_`x' mainact_student_assign_`x' mainact_student_uninv_`x' mainact_student_classmgt_`x'"
	local agg "mainact_student_mgmt_`x' mainact_student_offtask_`x' mainact_student_instruct_`x'"
	qui egen ind_`x'tot=rowtotal(`ind'), missing
	qui egen agg_`x'tot=rowtotal(`agg'), missing
	su ind_`x'tot agg_`x'tot
	drop *`x'x *`x'tot
}

/*total instructional time (any activity)*/
order student_instruct_*min mainact_student_instruct_*min
qui egen student_instruct_pct=rowmean(student_instruct_0min-student_instruct_48min)
qui egen student_instruct_pct_m0_27=rowmean(student_instruct_0min-student_instruct_24min)
qui egen student_instruct_pct_m28_52=rowmean(student_instruct_28min-student_instruct_48min)
qui egen student_instruct_pct_m0_11=rowmean(student_instruct_0min-student_instruct_8min)
qui egen student_instruct_pct_m12_27=rowmean(student_instruct_12min-student_instruct_24min)
qui egen student_instruct_pct_m28_39=rowmean(student_instruct_28min-student_instruct_36min)
qui egen student_instruct_pct_m40_52=rowmean(student_instruct_40min-student_instruct_48min)
foreach x in pct pct_m0_27 pct_m28_52 pct_m0_11 pct_m12_27 pct_m28_39 pct_m40_52 {
	qui replace student_instruct_`x'=. if student_active_`x'==. & student_passive_`x'==.
}

/*DON'T RESCALE "ANY ACTIVITY," BECAUSE LOGICALLY INCONSISTENT (totals can sum to >100%*/
order nummainact_student_*min multmainact_student_*min
qui egen nummainact_student_avg=rowmean(nummainact_student_0min-nummainact_student_36min)
qui egen multmainact_student_pct=rowmean(multmainact_student_0min-multmainact_student_36min)
qui lab var student_demo_pct "ETO: student proportion of time in demonstration/lecture (can sum to >100%)"
qui lab var student_qanda_pct "ETO: student proportion of time in Q&A/discussion (can sum to >100%)"
qui lab var student_present_pct "ETO: student proportion of time in student presentation (can sum to >100%)"
qui lab var student_pradrill_pct "ETO: student proportion of time in practice/drill (can sum to >100%)"
qui lab var student_pwork_pct "ETO: student proportion of time in project/interactive activity (can sum to >100%)"
qui lab var student_silentwork_pct "ETO: student proportion of time in silent seat work (can sum to >100%)"
qui lab var student_copying_pct "ETO: student proportion of time in copying (can sum to >100%)"
qui lab var student_assign_pct "ETO: student proportion of time in instructions for assignments (can sum to >100%)"
qui lab var student_uninv_pct "ETO: student proportion of time uninvolved (can sum to >100%)"
qui lab var student_classmgt_pct "ETO: student proportion of time in classroom management (can sum to >100%)"
qui lab var student_active_pct "ETO: student proportion of time in active instruction (can sum to >100%)"
qui lab var student_passive_pct "ETO: student proportion of time in passive instruction (can sum to >100%)"
qui lab var student_mgmt_pct "ETO: student proportion of time in classroom management (major category, can sum to >100%)"
qui lab var student_offtask_pct "ETO: student proportion of time off task (can sum to >100%)"
qui lab var student_instruct_pct "ETO: student proportion of time in instruction (can sum to >100%)"
qui lab var nummainact_student_avg "average number of student main activities"
qui lab var multmainact_student_pct "ETO: % of snapshots w/ multiple student main activities"	
	
foreach x in 0_27 28_52 0_11 12_27 28_39 40_52 {
	qui lab var student_demo_pct_m`x' "ETO: student proportion of time in demonstration/lecture, mins `x' (can sum to >100%)"
	qui lab var student_qanda_pct_m`x' "ETO: student proportion of time in Q&A/discussion, mins `x' (can sum to >100%)"
	qui lab var student_present_pct_m`x' "ETO: student proportion of time in student presentation, mins `x' (can sum to >100%)"
	qui lab var student_pradrill_pct_m`x' "ETO: student proportion of time in practice/drill, mins `x' (can sum to >100%)"
	qui lab var student_pwork_pct_m`x' "ETO: student proportion of time in project/interactive activity, mins `x' (can sum to >100%)"
	qui lab var student_silentwork_pct_m`x' "ETO: student proportion of time in silent seat work, mins `x' (can sum to >100%)"
	qui lab var student_copying_pct_m`x' "ETO: student proportion of time in copying, mins `x' (can sum to >100%)"
	qui lab var student_assign_pct_m`x' "ETO: student proportion of time in instructions for assignments, mins `x' (can sum to >100%)"
	qui lab var student_uninv_pct_m`x' "ETO: student proportion of time uninvolved, mins `x' (can sum to >100%)"
	qui lab var student_classmgt_pct_m`x' "ETO: student proportion of time in classroom management, mins `x' (can sum to >100%)"
	qui lab var student_active_pct_m`x' "ETO: student proportion of time in active instruction, mins `x' (can sum to >100%)"
	qui lab var student_passive_pct_m`x' "ETO: student proportion of time in passive instruction, mins `x' (can sum to >100%)"
	qui lab var student_mgmt_pct_m`x' "ETO: student proportion of time in classroom management, mins `x' (major category, can sum to >100%)"
	qui lab var student_offtask_pct_m`x' "ETO: student proportion of time off task, mins `x' (can sum to >100%)"
	qui lab var student_instruct_pct_m`x' "ETO: student proportion of time in instruction, mins `x' (can sum to >100%)"
}

qui lab var mainact_student_demo_pct "ETO: student main activity proportion of time in demonstration/lecture"
qui lab var mainact_student_qanda_pct "ETO: student main activity proportion of time in Q&A/discussion"
qui lab var mainact_student_present_pct "ETO: student main activity proportion of time in student presentation"
qui lab var mainact_student_pradrill_pct "ETO: student main activity proportion of time in practice/drill"
qui lab var mainact_student_pwork_pct "ETO: student main activity proportion of time in project/interactive activity"
qui lab var mainact_student_silentwork_pct "ETO: student main activity proportion of time in silent seat work"
qui lab var mainact_student_copying_pct "ETO: student main activity proportion of time in copying"
qui lab var mainact_student_assign_pct "ETO: student main activity proportion of time in instructions for assignments"
qui lab var mainact_student_uninv_pct "ETO: student main activity proportion of time uninvolved"
qui lab var mainact_student_classmgt_pct "ETO: student main activity proportion of time in classroom management"
qui lab var mainact_student_active_pct "ETO: student main activity proportion of time in active instruction"
qui lab var mainact_student_passive_pct "ETO: student main activity proportion of time in passive instruction"
qui lab var mainact_student_mgmt_pct "ETO: student main activity proportion of time in classroom management (major category)"
qui lab var mainact_student_offtask_pct "ETO: student main activity proportion of time off task"
qui lab var mainact_student_instruct_pct "ETO: student main activity proportion of time in instruction"

foreach x in 0_27 28_52 0_11 12_27 28_39 40_52 {
	qui lab var mainact_student_demo_`x' "ETO: student main activity proportion of time in demonstration/lecture, mins `x'"
	qui lab var mainact_student_qanda_`x' "ETO: student main activity proportion of time in Q&A/discussion, mins `x'"
	qui lab var mainact_student_present_`x' "ETO: student main activity proportion of time in student presentation, mins `x'"
	qui lab var mainact_student_pradrill_`x' "ETO: student main activity proportion of time in practice/drill, mins `x'"
	qui lab var mainact_student_pwork_`x' "ETO: student main activity proportion of time in project/interactive activity, mins `x'"
	qui lab var mainact_student_silentwork_`x' "ETO: student main activity proportion of time in silent seat work, mins `x'"
	qui lab var mainact_student_copying_`x' "ETO: student main activity proportion of time in copying, mins `x'"
	qui lab var mainact_student_assign_`x' "ETO: student main activity proportion of time in instructions for assignments, mins `x'"
	qui lab var mainact_student_uninv_`x' "ETO: student main activity proportion of time uninvolved, mins `x'"
	qui lab var mainact_student_classmgt_`x' "ETO: student main activity proportion of time in classroom management, mins `x'"
	qui lab var mainact_student_active_`x' "ETO: student main activity proportion of time in active instruction, mins `x'"
	qui lab var mainact_student_passive_`x' "ETO: student main activity proportion of time in passive instruction, mins `x'"
	qui lab var mainact_student_mgmt_`x' "ETO: student main activity proportion of time in classroom management (major category), mins `x'"
	qui lab var mainact_student_offtask_`x' "ETO: student main activity proportion of time off task, mins `x'"
	qui lab var mainact_student_instruct_`x' "ETO: student main activity proportion of time in instruction, mins `x'"
}

* SECTION 4: Classroom observation of curricular activities
qui gen curric_activity_el=(typeactivity_300==1)
lab var curric_activity_el "ETO300: observed curricular activity in classroom"

/*direct observation of classroom activities*/
foreach x in IntLearnAct SmallGroup IndepWork WorkPresent UseBooks PortfolioWork StudOutside CopyNotebooks Feedback {
	qui gen observe_`x'_el=(observations_300_`x'==1)
	lab var observe_`x'_el "ETO300: observed `x' work in classroom"
}

local x "observations_300_IntLearnAct observations_300_SmallGroup observations_300_IndepWork observations_300_WorkPresent observations_300_UseBooks observations_300_PortfolioWork observations_300_StudOutside observations_300_Feedback"
qui egen observe_interactive_el=anymatch(`x'), v(1)
qui egen observe_interactive_num_el=rowtotal(`x')
lab var observe_interactive_el "ETO300: observed at least one interactive pedagogical technique with curriculum"
lab var observe_interactive_num_el "ETO300: number of interactive pedagogical techniques observed (of 8)"

/*portfolios, display of student work*/
qui gen teacher_assignportf_el=(teacher_assignw_300x==2|teacher_assignw_300x==3)
lab var teacher_assignportf_el "ETO301: teacher assigned work for student portfolio"
qui gen studentwork_notdisplayed_el=(displaystudentwork_300x1==0)
qui gen studentwork_displayed_el=(displaystudentwork_300x1==1|displaystudentwork_300x1==3)
qui gen studentwork_board_el=(displaystudentwork_300x1==1)
qui gen studentwork_noevidence_el=(displaystudentwork_300x1==3)
lab var studentwork_notdisplayed_el "ETO302: student work not visibly displayed in classroom"
lab var studentwork_displayed_el "ETO302: student work visibly displayed in classroom"
lab var studentwork_board_el "ETO302: student work visibly displayed on board in classroom"
lab var studentwork_noevidence_el "ETO302: student work visibly displayed, but no evidence provided"

/*physical layout of classroom*/
qui gen layout_notinteract_el=(class_phyc_303_e==1)
qui gen layout_interact_grpwrkonly_el=(class_phyc_303_e==4)
qui gen layout_interact_always_el=(class_phyc_303_e==2|class_phyc_303_e==3|class_phyc_303_e==5)
lab var layout_notinteract_el "ET0303: classroom layout not interactive (desks in rows)"
lab var layout_interact_grpwrkonly_el "ET0303: classroom layout interactive only during group work"
lab var layout_interact_always_el "ET0303: classroom layout interactive (circle or desks facing each other)"

/*learning aids*/
local aids "learning_aid_304_e_studportflio learning_aid_304_e_prestboards learning_aid_304_e_visualaids learning_aid_304_e_worksheets"
qui egen learningaidsnum_el=rowtotal(`aids')
qui gen learningaidspct_el=learningaidsnum_el/4
lab var learningaidsnum_el "ETO304: # of learning aids in classroom (of 4)"
lab var learningaidspct_el "ETO304: % of learning aids in classroom (of 4)"
notes learningaidsnum_el: "learning aids: portfolio, present boards, visual aids, worksheets"
notes learningaidspct_el: "learning aids: portfolio, present boards, visual aids, worksheets"

/*business clubs, group work, student presentations*/
qui gen busclubref_el=(bus_club_305==1)
qui gen grpwrkengage_el=(group_work_306_e==3|group_work_306_e==4)
qui gen studpresent_el=(present_307_e==1)
qui gen presentengage_el=(yes_present_308_e_teachask==1|yes_present_308_e_teachresp==1)
lab var busclubref_el "ETO305: teacher mentioned student business clubs in class"
lab var grpwrkengage_el "ETO306: teacher engaged with students during group work"
lab var studpresent_el "ETO307: students presented to class"
lab var presentengage_el "ETO308: teacher engaged w/ student presenters (responded or asked questions)"


forval i=1/3 {
	local j=`i'+3
	qui gen class_S`j'_el=(class_301==`i')
	lab var class_S`j'_el "ETO309: observed class is S`j'"
}

/*teaching methods*/
/*can also specific topics within these methods*/
local X "groupdiscussion research casestudy roleplay finance"
foreach x in `X' {
	qui egen method_`x'_el=anymatch(`x'_3??), v(1)
	lab var method_`x'_el "ETO3xx: used `x' as teaching method"
}

local X "method_groupdiscussion method_research method_casestudy method_roleplay method_finance"
qui egen method_curric_el=anymatch(`X'), v(1)
qui egen method_curric_num_el=rowtotal(`X')
qui gen method_curric_pct_el=method_curric_num_el/5
lab var method_curric_el "ETO307-325: observed at least one curricular-specific method (of 5) "
lab var method_curric_num_el "ETO307-325: # curricular-specific methods observed (of 5)"
lab var method_curric_pct_el "ETO307-325: % curricular-specific methods observed (of 5)"

* save data
qui gen insample_obs_el=1
lab var insample_obs_el "in endline teacher classroom observation sample"

qui compress
lab data "Teacher endline observation (2018), modified from cleaned data"
qui save "$cleandata/teacherobs_endline_jde.dta", replace	

local end=`"$S_TIME"' 
di "`start'"
di "`end'"
